from research.factor import factordata
from jili.data.db import getdb_client,tradecal,get_calender
from jili.core.convert import str2datetime
from copy import deepcopy
def mongo2factor(db,cuname,factor_key,factor_name,factor_url,timekey,timekey_value=None,start_date=None,isusepre=True):
    f=factordata(name=factor_name,url=factor_url)
    maxdate=f.get_maxtimekey()
    predata=f.get_bydate(maxdate)
    if start_date is not None and maxdate is None:
        maxdate=str2datetime(start_date)
    elif start_date is not None and maxdate is not None:
        maxdate = tradecal.getnexttradeday(maxdate)
        start_date=str2datetime(start_date)
        if start_date<maxdate:
            maxdate=start_date

    db=getdb_client(dbname=db)
    cu=db[cuname]
    d=None
    for i in cu.find({}).sort([(timekey,-1)]):
        d=str2datetime(i[timekey])
        break
    data={}
    if maxdate is None:
        maxdate=tradecal.gettradeday("20150101")
    if d>=maxdate:
        if timekey_value:
            find={timekey:{"$gte":maxdate.strftime(timekey_value)}}
        else:
            find={timekey:{"$gte":maxdate}}
        for i in cu.find(find).sort([(timekey,1)]):
            d=i[timekey]
            k=str2datetime(d)
            obj=i["obj"]
            if k not in data.keys():
                data[k]={}
            data[k][obj]=i[factor_key]
        if isusepre:
            ds=list(data.keys())
            ds.sort()
            for d in get_calender(ds[0],ds[-1]):
                if d not in data.keys():
                    data[d]=deepcopy(predata)
                else:
                    for obj,v in data[d].items():
                        predata[obj]=v
                    data[d] = deepcopy(predata)
        f.save_all(data,name=factor_name)